package com.daJiao.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.daJiao.domain.*;
import com.daJiao.entity.SysUser;
import com.daJiao.holder.SecurityUserHolder;
import com.daJiao.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>Title:      SysUserController. </p>
 * <p>Description 用户controller </p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company:    北京医百科技</p>
 *
 * @author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
 * @since 2021/12/5 10:56
 */
@RestController
@RequestMapping(value = "/sysUser")
@Api(tags = "用户管理")
public class SysUserController {

    @Resource
    private SysUserService userService;

    @Resource
    private SecurityUserHolder securityUserHolder;

    /**
     * <p>Title:	  get. </p>
     * <p>Description 根据id查询用户信息</p>
     *
     * @param id String
     * @return the wrapper
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @CreateDate 2021 /12/17 19:04
     */
    @GetMapping("/{id}")
    @ApiOperation(notes = "根据id查询用户信息", httpMethod = "GET", value = "根据id查询用户信息")
    public Wrapper<SysUser> get(@PathVariable String id) {
        SysUser user = this.userService.getById(id);
        return WrapMapper.ok(user);
    }

    /**
     * <p>Title:	  listPage. </p>
     * <p>Description 分页查询用户列表</p>
     *
     * @param pageParam the PageParam
     * @return the wrapper
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @CreateDate 2021 /12/17 19:04
     */
    @PostMapping(value = "/page")
    @ApiOperation(httpMethod = "POST", value = "分页查询用户列表")
    public Wrapper<Page<SysUser>> page(@ApiParam(name = "user", value = "用户信息") @RequestBody PageParam<SysUser> pageParam) {
        Page<SysUser> listPage = userService.listPage(pageParam);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, listPage);
    }

    /**
     * <p>Title:      getCurrentUser. </p>
     * <p>Description 获取当前用户信息</p>
     *
     * @param
     * @return current user
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @since 2021 /12/17 19:04
     */
    @GetMapping(value = "/getCurrentUser")
    @ApiOperation(httpMethod = "GET", value = "获取当前用户信息")
    public Wrapper<SecurityUser> getCurrentUser() {
        SecurityUserDetails securityUser = securityUserHolder.getUser();
        return WrapMapper.ok(userService.getCurrentUser(securityUser));
    }

    /**
     * <p>Title:      saveUserInfo. </p>
     * <p>Description 保存用户信息</p>
     *
     * @param user the user
     * @return wrapper
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @since 2021 /12/17 19:04
     */
    @PostMapping(value = "/save")
    @ApiOperation(httpMethod = "POST", value = "保存用户信息")
    public Wrapper<Integer> save(@ApiParam(name = "user", value = "用户信息") @RequestBody @Validated(CreateValidate.class) SysUser user) {
        SecurityUserDetails securityUser = securityUserHolder.getUser();
        user.setCreateBy(securityUser.getUserId());
        user.setCreator(securityUser.getRealName());
        user.setUpdateBy(securityUser.getUserId());
        user.setUpdater(securityUser.getRealName());
        int result = userService.saveUser(user, securityUser);
        return WrapMapper.ok(result);
    }

    /**
     * <p>Title:      update. </p>
     * <p>Description 修改用户信息</p>
     *
     * @param user the user
     * @return wrapper
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @since 2021 /12/17 19:04
     */
    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改用户信息")
    public Wrapper<Integer> update(@ApiParam(name = "user", value = "用户信息") @RequestBody @Validated(UpdateValidate.class) SysUser user) {
        SecurityUserDetails securityUser = securityUserHolder.getUser();
        user.setUpdateBy(securityUser.getUserId());
        user.setUpdater(securityUser.getRealName());
        return WrapMapper.ok(userService.updateUserInfo(user));
    }

    /**
     * <p>Title:      delete. </p>
     * <p>Description 根据id删除用户</p>
     *
     * @param id the id
     * @return wrapper
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @since 2021 /12/17 19:04
     */
    @DeleteMapping(value = "/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除用户")
    public Wrapper<Integer> delete(@PathVariable String id) {
        SecurityUserDetails securityUser = securityUserHolder.getUser();
        return WrapMapper.ok(userService.delete(id, securityUser));
    }

    /**
     * <p>Title:      userBindRole. </p>
     * <p>Description 用户绑定角色</p>
     *
     * @param userBindRole UserBindRole
     * @return Boolean
     * @Author <a href="yangwensheng@100doc.com.cn"/>杨文生</a>
     * @since 2019/4/10 20:18
     */
    @PostMapping(value = "/bind/role")
    @ApiOperation(httpMethod = "POST", value = "用户绑定角色")
    public Wrapper<Boolean> userBindRole(@RequestBody UserBindRole userBindRole) {
        SecurityUserDetails securityUser = securityUserHolder.getUser();
        userService.userBindRole(userBindRole, securityUser);
        return WrapMapper.ok();
    }

    /**
     * 获取用户绑定的角色信息
     *
     * @param userId String
     * @return List<String
     */
    @GetMapping(value = "/bind/role/{userId}")
    @ApiOperation(httpMethod = "GET", value = "获取用户绑定的角色信息")
    public Wrapper<List<String>> getBindRoleByUserId(@PathVariable String userId) {
        List<String> roleIds = userService.getBindRoleByUserId(userId);
        return WrapMapper.ok(roleIds);
    }

}
